package com.zmn.plat.admin.controller.product.cityratio;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import com.zmn.common.constant.GlobalConsts;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.zmn.common.constant.StatusConsts;
import com.zmn.common.dto.ResultDTO;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.plat.business.interfaces.product.BaseCityPriceRatioBService;
import com.zmn.plat.common.constant.ProductConsts;
import com.zmn.plat.common.dictionary.BaseCityRatioTypeEnum;
import com.zmn.plat.model.entity.base.city.ratio.BaseCityRatio;
import com.zmn.plat.model.vo.product.BaseCityCategoryRatioModifyVo;
import com.zmn.plat.model.vo.product.BaseCityRatioVo;
import com.zmn.plat.model.vo.product.cityRatio.CityRatioBatchVO;

/**
 * 类描述:城市价格系数管理修改控制
 *
 * @author linfeng
 * @since 2019/08/21/11:02
 */
@Controller
@RequestMapping("/product/citypriceratio/")
public class ServCityPriceRatioModifyController extends ServCityPriceRatioBaseController {
    private Logger logger = LoggerFactory.getLogger(ServCityPriceRatioModifyController.class);

    @Autowired
    private BaseCityPriceRatioBService baseCityPriceRatioBService;

    /**
     * 添加/修改城市默认系数 bizType 1保外 2保内 3工程
     *
     * @param request
     * @param baseCityRatio
     * @return
     */
    @RequestMapping("addEdit")
    @ResponseBody
    @SuppressWarnings("Duplicates")
    public ResponseDTO addEdit(HttpServletRequest request, @RequestBody BaseCityRatioVo baseCityRatio) {
        Date now = DateUtil.getNow();

        try {
            BaseCityRatio baseCityRatioC = baseCityRatioService.findByRatioTypeAndCityAndCategAndBizType(baseCityRatio.getPriceType(),
                BaseCityRatioTypeEnum.CITYRATIO.getCode(), baseCityRatio.getCityId(), null, null, null, ProductConsts.BIZ_TYPE_C);
            BaseCityRatio baseCityRatioF = baseCityRatioService.findByRatioTypeAndCityAndCategAndBizType(baseCityRatio.getPriceType(),
                BaseCityRatioTypeEnum.CITYRATIO.getCode(), baseCityRatio.getCityId(), null, null, null, ProductConsts.BIZ_TYPE_F);
            BaseCityRatio baseCityRatioB = baseCityRatioService.findByRatioTypeAndCityAndCategAndBizType(baseCityRatio.getPriceType(),
                BaseCityRatioTypeEnum.CITYRATIO.getCode(), baseCityRatio.getCityId(), null, null, null, ProductConsts.BIZ_TYPE_B);
            BaseCityRatio baseCityRatioM = baseCityRatioService.findByRatioTypeAndCityAndCategAndBizType(baseCityRatio.getPriceType(),
                    BaseCityRatioTypeEnum.CITYMETER3RATIO.getCode(), baseCityRatio.getCityId(), null, null, null, null);

            BaseCityRatio cityRatio = new BaseCityRatio();
            cityRatio.setCreater(getMcStaffRealName());
            cityRatio.setCreateTime(now);
            cityRatio.setUpdater(getMcStaffRealName());
            cityRatio.setUpdateTime(now);
            cityRatio.setCityId(baseCityRatio.getCityId());
            cityRatio.setProvinceId(baseCityRatio.getProvinceId());
            cityRatio.setBizType(ProductConsts.BIZ_TYPE_C);
            cityRatio.setPriceType(baseCityRatio.getPriceType());

            if (baseCityRatioM == null) {
                if (baseCityRatio.getCityMeterToBeEffectiveRatio() != null) {
                    cityRatio.setBizType(null);
                    cityRatio.setRatioType(BaseCityRatioTypeEnum.CITYMETER3RATIO.getCode());
                    cityRatio.setEffectiveDate(baseCityRatio.getEffectiveDate());
                    cityRatio.setToBeEffectiveRatio(baseCityRatio.getCityMeterToBeEffectiveRatio());
                    baseCityRatioService.insert(cityRatio);
                }
            } else {
                if (baseCityRatioM.getRatio() == null && baseCityRatio.getCityMeterToBeEffectiveRatio() == null) {
                    baseCityRatioService.deleteByKey(baseCityRatioB.getRatioId());
                } else {
                    baseCityRatioM.setToBeEffectiveRatio(baseCityRatio.getCityMeterToBeEffectiveRatio());
                    baseCityRatioM.setUpdateTime(DateUtil.getNow());
                    baseCityRatioM.setUpdater(getMcStaffRealName());
                    baseCityRatioM.setEffectiveDate(baseCityRatio.getEffectiveDate());
                    baseCityRatioService.updateByKey(baseCityRatioM);
                }
            }

            if (baseCityRatioC == null) {
                if (baseCityRatio.getCityOutToBeEffectiveRatio() != null) {
                    cityRatio.setBizType(ProductConsts.BIZ_TYPE_C);
                    cityRatio.setRatioType(BaseCityRatioTypeEnum.CITYRATIO.getCode());
                    cityRatio.setToBeEffectiveRatio(baseCityRatio.getCityOutToBeEffectiveRatio());
                    cityRatio.setEffectiveDate(baseCityRatio.getEffectiveDate());
                    baseCityRatioService.insert(cityRatio);
                }
            } else {
                if (baseCityRatioC.getRatio() == null && baseCityRatio.getCityOutToBeEffectiveRatio() == null) {
                    baseCityRatioService.deleteByKey(baseCityRatioC.getRatioId());
                } else {
                    baseCityRatioC.setToBeEffectiveRatio(baseCityRatio.getCityOutToBeEffectiveRatio());
                    baseCityRatioC.setEffectiveDate(baseCityRatio.getEffectiveDate());
                    baseCityRatioC.setUpdateTime(DateUtil.getNow());
                    baseCityRatioC.setUpdater(getMcStaffRealName());
                    baseCityRatioService.updateByKey(baseCityRatioC);
                }
            }

            if (baseCityRatioF == null) {
                if (baseCityRatio.getCityInToBeEffectiveRatio() != null) {
                    cityRatio.setBizType(ProductConsts.BIZ_TYPE_F);
                    cityRatio.setRatioType(BaseCityRatioTypeEnum.CITYRATIO.getCode());
                    cityRatio.setToBeEffectiveRatio(baseCityRatio.getCityInToBeEffectiveRatio());
                    cityRatio.setEffectiveDate(baseCityRatio.getEffectiveDate());
                    baseCityRatioService.insert(cityRatio);
                }
            } else {
                if (baseCityRatioF.getRatio() == null && baseCityRatio.getCityInToBeEffectiveRatio() == null) {
                    baseCityRatioService.deleteByKey(baseCityRatioF.getRatioId());
                } else {
                    baseCityRatioF.setToBeEffectiveRatio(baseCityRatio.getCityInToBeEffectiveRatio());
                    baseCityRatioF.setUpdateTime(DateUtil.getNow());
                    baseCityRatioF.setUpdater(getMcStaffRealName());
                    baseCityRatioF.setEffectiveDate(baseCityRatio.getEffectiveDate());
                    baseCityRatioService.updateByKey(baseCityRatioF);
                }
            }

            if (baseCityRatioB == null) {
                if (baseCityRatio.getCityProjectToBeEffectiveRatio() != null) {
                    cityRatio.setBizType(ProductConsts.BIZ_TYPE_B);
                    cityRatio.setRatioType(BaseCityRatioTypeEnum.CITYRATIO.getCode());
                    cityRatio.setEffectiveDate(baseCityRatio.getEffectiveDate());
                    cityRatio.setToBeEffectiveRatio(baseCityRatio.getCityProjectToBeEffectiveRatio());
                    baseCityRatioService.insert(cityRatio);
                }
            } else {
                if (baseCityRatioB.getRatio() == null && baseCityRatio.getCityProjectToBeEffectiveRatio() == null) {
                    baseCityRatioService.deleteByKey(baseCityRatioB.getRatioId());
                } else {
                    baseCityRatioB.setToBeEffectiveRatio(baseCityRatio.getCityProjectToBeEffectiveRatio());
                    baseCityRatioB.setUpdateTime(DateUtil.getNow());
                    baseCityRatioB.setUpdater(getMcStaffRealName());
                    baseCityRatioB.setEffectiveDate(baseCityRatio.getEffectiveDate());
                    baseCityRatioService.updateByKey(baseCityRatioB);
                }
            }

            return ResponseDTO.success(GlobalConsts.YES,"操作成功！");
        } catch (Exception ex) {
            logger.error(
                String.format("[zmn]新增/修改城市价格系数失败,baseCityRatio:[%s],ex:[%s]", JSONObject.toJSONString(baseCityRatio), ex.getMessage()));
            return ResponseDTO.fail("操作失败！");
        }
    }

    /**
     * 添加或修改城市分类系数
     *
     * @param request
     * @param baseCityCategoryRatioModifyVo
     * @return
     */
    @RequestMapping("categoryRatioAddEdit")
    @ResponseBody
    public ResponseDTO addEditCategoryRatio(HttpServletRequest request,
        @RequestBody BaseCityCategoryRatioModifyVo baseCityCategoryRatioModifyVo) {
        baseCityPriceRatioBService.addEditCategoryRatio(getMcStaffRealName(), baseCityCategoryRatioModifyVo);
        return ResponseDTO.success(StatusConsts.STATUS_SUCCESS, "操作成功");
    }

    /**
     * 添加或修改城市保外分类系数
     *
     * @param request
     * @param cityRatioBatchVO
     * @return
     */
    @RequestMapping("saveCityRatioBatch")
    @ResponseBody
    public com.zmn.oms.common.dto.ResultDTO saveCityOutRatioBatch(HttpServletRequest request,
        @RequestBody CityRatioBatchVO cityRatioBatchVO) {
        baseCityPriceRatioBService.saveCityRatioBatch(getMcStaff(), cityRatioBatchVO);
        return com.zmn.oms.common.dto.ResultDTO.success(true, "设置成功");
    }

    /**
     * 添加或修改城市分类系数
     *
     * @param request
     * @param cityRatioBatchVO
     * @return
     */
    @RequestMapping("saveCityCategRatioBatch")
    @ResponseBody
    public com.zmn.oms.common.dto.ResultDTO saveCityCategRatioBatch(HttpServletRequest request,
        @RequestBody CityRatioBatchVO cityRatioBatchVO) {
        baseCityPriceRatioBService.saveCityCategRatioBatch(getMcStaff(), cityRatioBatchVO);
        return com.zmn.oms.common.dto.ResultDTO.success(true, "设置成功");
    }
}
